//
//  PickUpViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/11/24.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "PickUpViewController.h"
#import "PickGoodsViewController.h"

@interface PickUpViewController ()
@property (nonatomic,strong) UITextField *nameTF;
@property (nonatomic,strong) UIButton *doneBtn;
@property (nonatomic,strong) PickGoodsViewController *pickGoodsVC;

@end

@implementation PickUpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    if (![self.bank isEqualToString:@"bank"]) {
        self.view.backgroundColor =  HexWithAlpha(0x000000, 0.6);
 //   }
    [self setMainView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TextFieldChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setMainView
{
    UIView *backView = [UIView new];
    backView.frame = CGRectMake(50, 200, SCREENWIDTH-100, 180);
    backView.backgroundColor = TotalWhite;
    backView.layer.cornerRadius = 12;
    [self.view addSubview:backView];
    
    UILabel *titleLab = [UILabel new];
    titleLab.frame = CGRectMake(0, 20, SCREENWIDTH-100, 14);
    titleLab.textColor = titleColor;
    titleLab.font = FontSemiboldSize(14);
    titleLab.text = @"请输入取件码";
    titleLab.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:titleLab];
    
    UIImageView *close = [UIImageView new];
    close.frame = CGRectMake(backView.width-36, 20, 16, 16);
    close.image = [UIImage imageNamed:@"anClose"];
    [backView addSubview:close];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(close.left-5, 15, 26, 26);
    [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:closeBtn];
    
    UITextField *tf = [UITextField new];
    tf.frame = CGRectMake(leftPadding, titleLab.bottom+25, backView.width-40, 40);
    tf.backgroundColor = UIColorFromHex(0xF4F4F4);
    tf.keyboardType = UIKeyboardTypeNumberPad;
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.placeholder = @" 请输入4位取件码";
    [tf setValue:UIColorFromHex(0xD3D3D3) forKeyPath:@"_placeholderLabel.textColor"];
    [tf setValue:FontWithSize(12) forKeyPath:@"_placeholderLabel.font"];
    tf.textColor = titleColor;
    tf.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    self.nameTF = tf;
    [backView addSubview:tf];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, backView.height-44, backView.width, 44);
    btn.backgroundColor = UIColorFromHex(0xF7D553);
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    btn.titleLabel.font = FontSemiboldSize(12);
    //btn.layer.cornerRadius = 12;
    //设置按钮的下部分切圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn.layer.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10.f,10.f)];
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = btn.layer.bounds;
    maskLayer.path = maskPath.CGPath;
    btn.layer.mask = maskLayer;
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    self.doneBtn = btn;
    [backView addSubview:btn];
}

- (void)TextFieldChange
{
    NSString *code = self.nameTF.text;
    if (code.length > 6) {
        code = [code substringToIndex:6];
    }
}

- (void)ShowHUDWithMessage:(NSString *)message
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.bezelView.backgroundColor = UIColorFromHex(0xFF9D9D9D);
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = NSLocalizedString(message, nil);
    hud.label.font = FontWithSize(12);
    [hud hideAnimated:YES afterDelay:HUDTime];
}

- (void)next
{
    //先删除本界面
   // [self.view removeFromSuperview];
    [self.nameTF resignFirstResponder];
    if ([self.bank isEqualToString:@"bank"]) {
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:self.nameTF.text forKey:@"verificationCode"];
        [HttpRequest formPostPath:Change_validate_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
            [self ShowHUDWithMessage:@"换绑银行卡成功"];
            [self.view removeFromSuperview];
        }];
    }
    else
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:self.nameTF.text forKey:@"code"];
        [HttpRequest formPostPath:GetByCode_API params:dict resultBlock:^(id Data, MsgModel *msgModel) {
            if ([Data[@"code"]integerValue] == 200 && ![TTKUtil isNil:[Data objectForKey:@"result"]])
            {
                NSArray *myArray = Data[@"result"][@"orderItemDetails"];
                self.pickGoodsVC = [[PickGoodsViewController alloc]init];
                self.pickGoodsVC.vc = self;
                self.pickGoodsVC.orderId = [Data[@"result"][@"id"]integerValue];
                self.pickGoodsVC.code = self.nameTF.text;
                self.pickGoodsVC.dict = [myArray firstObject];
                [self.view addSubview:self.pickGoodsVC.view];
                //[[UIApplication sharedApplication].windows[0] addSubview:self.pickVC.view];
                self.pickGoodsVC.view.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
            }
            else
            {
                [TTKUtil alertControllerShowMsg:Data[@"message"] and:self];
            }
        }];
    }
}

- (void)close
{
    if ([self.bank isEqualToString:@"bank"]) {
        [self.view removeFromSuperview];
        self.changeVC.view.hidden = NO;
    }
    else
    {
        [self.view removeFromSuperview];
    }
}

@end
